package com.ehome.poct.terminal.controller;

import com.ehome.core.page.PageInfo;
import com.ehome.poct.base.api.dto.doctor.ClinicDTO;
import com.ehome.poct.base.api.feign.ClinicClient;
import com.ehome.poct.core.api.dto.DelReportDTO;
import com.ehome.poct.core.api.dto.ReportListDTO;
import com.ehome.poct.core.api.dto.check.ReportDTO;
import com.ehome.poct.core.api.dto.report.*;
import com.ehome.poct.core.api.feign.ReportClient;
import com.ehome.poct.core.api.vo.CheckReportPageVO;
import com.ehome.poct.core.api.vo.ReportDetailVO;
import com.ehome.poct.core.api.vo.ReportDtlVO;
import com.ehome.poct.core.api.vo.report.TestReportConfRecordVO;
import com.ehome.poct.core.api.vo.report.TestReportConfVO;
import com.ehome.poct.terminal.service.ReportService;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author muqiu
 * @date 2022/03/03 10:25
 **/
@Validated
@Api(tags = "检测报告")
@ApiSupport(author = "muqiu")
@RequestMapping("/report")
@RestController
@AllArgsConstructor
public class ReportController {
    private final ReportClient reportClient;
    private final ClinicClient clinicClient;
    private final Map<String, ReportService> reportService;

    private final Map<Integer, String> deviceTypeMap = Map.ofEntries(
            Map.entry(1, "bldReportServiceImpl"),
            Map.entry(2, "jbyReportServiceImpl")
    );


    @ApiOperation("查询检测列表")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping("/list")
    public CheckReportPageVO list(@RequestBody @Valid ReportListDTO dto) {
        return reportService.get(deviceTypeMap.get(dto.getDeviceType())).list(dto);
    }

    @ApiOperation("根据订单号查询血常规报告详情")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/by/order/{orderNo}")
    public ReportDetailVO getOneByOrderNo(@PathVariable String orderNo) {
        ReportDetailVO reportDetailVO = reportClient.getCheckReportByOrderNo(orderNo);
        ClinicDTO clinicDTO = clinicClient.getById(reportDetailVO.getPatient().getClinicId());
        reportDetailVO.setClinicName(clinicDTO.getName());
        return reportDetailVO;
    }

    @ApiOperation("根据报告ID查询血常规报告详情")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/{reportId}")
    public ReportDetailVO getOne(@PathVariable Long reportId) {
        ReportDetailVO checkReport = reportClient.getCheckReport(reportId);
        ClinicDTO clinicDTO = clinicClient.getById(checkReport.getPatient().getClinicId());
        checkReport.setClinicName(clinicDTO.getName());
        return checkReport;
    }

    @ApiOperation("删除检测报告")
    @ApiOperationSupport(author = "muqiu")
    @DeleteMapping("/delete")
    @RequestLog
    public void list(@RequestBody DelReportDTO delReportDTO) {
        reportService.get(deviceTypeMap.get(delReportDTO.getDeviceType())).deleteById(delReportDTO);
    }

    /**
     * 金标仪单项检测报告详情
     *
     * @param id
     * @return
     */
    @ApiOperation("报告ID查询金标仪单项检测报告详情")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/getSingleDetail/{id}")
    public ReportDtlVO getSingleOne(@PathVariable("id") Long id) {
        ReportDtlVO singleOne = reportClient.getSingleOne(id);
        ClinicDTO clinicDTO = clinicClient.getById(singleOne.getPatient().getClinicId());
        singleOne.setClinicName(clinicDTO.getName());
        return singleOne;
    }

    /**
     * 联合检测报告详情
     *
     * @param id
     * @return
     */
    @ApiOperation("获取联合检测报告详情")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/getUnionDetail/{id}")
    public ReportDtlVO getUnionOne(@PathVariable("id") Long id) {
        ReportDtlVO unionOne = reportClient.getUnionOne(id);
        ClinicDTO clinicDTO = clinicClient.getById(unionOne.getPatient().getClinicId());
        unionOne.setClinicName(clinicDTO.getName());
        return unionOne;
    }

    @ApiOperation("根据订单号报告打印数据（deviceType :1.血常规 2.金标仪）")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/by/orderPrint/{orderNo}/{local}/{deviceType}")
    public ReportDetailVO getOneByOrderNoPrint(@PathVariable(value = "orderNo") String orderNo, @PathVariable("local") Boolean local, @PathVariable("deviceType") Integer deviceType) {
        return reportService.get(deviceTypeMap.get(deviceType)).getOneByOrderNoPrint(orderNo, local);
    }

    /**
     * 告知金标仪血常规打印机状态
     *
     * @param clinicId
     */
    @ApiOperation("告知打印机状态（deviceType :1.血常规 2.金标仪）")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/getPrinterStatus/{clinicId}/{status}/{deviceType}")
    public void getPrinterStatus(@PathVariable(value = "clinicId") Long clinicId, @PathVariable(value = "status") String status, @PathVariable(value = "deviceType") Integer deviceType) {
        reportService.get(deviceTypeMap.get(deviceType)).toSendStatus(clinicId, status);
    }

    @ApiOperation("生成报告")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping(value = "/createReport")
    @RequestLog
    public void createReport(@RequestBody ReportReqDTO reportReqDTO) {
        reportService.get(deviceTypeMap.get(reportReqDTO.getDeviceType())).createReport(reportReqDTO);
    }

    /**
     * 获取报告列表
     *
     * @param orderNo
     * @return
     */
    @ApiOperation("根据订单获取报告列表")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/getByOrderNo/{orderNo}")
    public List<ReportDTO> getSummary(@PathVariable("orderNo") String orderNo) {
        return reportClient.simpleReportList(orderNo);
//        //金标仪检测报告
//        List<ReportDTO> jbyReport = reportService.get(deviceTypeMap.get(2)).getSummary(orderNo);
//        //血常规报告和联测报告
//        List<ReportDTO> bldReport = reportService.get(deviceTypeMap.get(1)).getSummary(orderNo);
//        jbyReport.addAll(bldReport);
//        return jbyReport;
    }

    /**
     * 报告的隐藏和展示设置
     *
     * @param reportConfigDTO
     * @return
     */
    @ApiOperation("报告的隐藏和展示设置")
    @ApiOperationSupport(author = "muqiu")
    @PostMapping("/reportConfig")
    @RequestLog
    public void reportConf(@RequestBody ReportConfigDTO reportConfigDTO) {
        //金标仪检测报告隐藏与展示
        reportService.get(deviceTypeMap.get(2)).reportConf(reportConfigDTO);
        //血常规报告报告隐藏与展示
        reportService.get(deviceTypeMap.get(1)).reportConf(reportConfigDTO);
    }

    @ApiOperation("报告的配置列表")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/reportConfigPage")
    public PageInfo<TestReportConfVO> reportConfPage(@SpringQueryMap ReportConfigQry reportConfigQry) {
        return reportService.get(deviceTypeMap.get(2)).reportConfPage(reportConfigQry);
    }

    @ApiOperation("报告的配置更改记录")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/reportConfRecord")
    public PageInfo<TestReportConfRecordVO> reportConfRecord(@SpringQueryMap ReportConfigRecordDTO reportConfigRecordDTO) {
        return reportService.get(deviceTypeMap.get(2)).reportConfRecord(reportConfigRecordDTO);
    }

    @ApiOperation("匹配诊所信息")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/clinicInfo/{account}")
    public ClinicInfoVO clinicInfo(@PathVariable String account) {
        return reportService.get(deviceTypeMap.get(2)).clinicInfo(account);
    }
}